package cn.huobi.order.controller;

import cn.huobi.basic.util.AjaxResult;
import cn.huobi.basic.util.PageList;
import cn.huobi.order.domain.OrderPetAcquisition;
import cn.huobi.order.dto.OrderConfirmDto;
import cn.huobi.order.query.OrderPetAcquisitionQuery;
import cn.huobi.order.service.IOrderPetAcquisitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {
    @Autowired
    public IOrderPetAcquisitionService orderPetAcquisitionService;


    /**
     * 保存和修改公用的
     *
     * @param orderPetAcquisition 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderPetAcquisition orderPetAcquisition) {
        try {
            if (orderPetAcquisition.getId() != null)
                orderPetAcquisitionService.update(orderPetAcquisition);
            else
                orderPetAcquisitionService.add(orderPetAcquisition);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            orderPetAcquisitionService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public OrderPetAcquisition get(@PathVariable("id") Long id) {
        return orderPetAcquisitionService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<OrderPetAcquisition> list() {

        return orderPetAcquisitionService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<OrderPetAcquisition> json(@RequestBody OrderPetAcquisitionQuery query) {
        return orderPetAcquisitionService.queryByPage(query);
    }

    /**
     * 交易成功
     * @param orderConfirmDto
     * @return
     */
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody OrderConfirmDto orderConfirmDto) {
        orderPetAcquisitionService.confirm(orderConfirmDto);
        return AjaxResult.me().success();
    }

    /**
     * 交易失败，取消订单
     * @param orderId
     * @return
     */
    @PostMapping("/cancel/{orderId}")
    public AjaxResult confirm(@PathVariable("orderId") Long orderId) {
        orderPetAcquisitionService.cancel(orderId);
        return AjaxResult.me().success();
    }
}
